<?php
namespace User\Controller;
use Think\Controller;

class ApiController extends Controller{

    /**
	 * 执行登录,状态-1为禁用账户，状态0表示未激活，状态1表示正常账户
	 */
	public function login(){
		
		if(!I('username') || !I('password')){
			_apiReturn(0, L('error_info_login_1'));
		}
		
		$m = D('Users');
		
		$map['username|email|mobile'] = I('username');
		$temp = $m->where($map)->find();
		if(!$temp){
			_apiReturn(0, "用户名不存在");
		}
		
		$salt = $temp['salt'];
		$map['password'] = md5(md5(I('password')).$salt);
		$map['status'] = array('neq',0); //状态不能为0
		$user = $m->where($map)->find();
		if($user){
			if($user['status'] == -1){
				if(LANG_SET == 'zh-cn'){
					_apiReturn(0,'对不起，该账户已被管理员禁用');
				}else{
					_apiReturn(0,'Sorry, The account has been disabled by the administrator');
				}
			}
			if($user['status'] == 0){
				//检测一下是不是状态为0的用户
				$_SESSION['tmpUser'] = $user; //存储在tmpUser session里面
				if(LANG_SET == 'zh-cn'){
					_apiReturn(0,"该账户尚未激活，请激活后再使用！",'','/user/reg/regnext?type=email'); //去激活
				}else{
					_apiReturn(0,'The account has not yet been activated','','/user/reg/regnext?type=email'); //去激活
				}
			}
			//登录成功
			//D('UserLog')->_add($user['uid'],'login','APP登录平台'); //写入日志
			
			//生成session
			unset($user['password']);
			unset($user['create_time']);
			unset($user['update_time']);
			//$_SESSION['loginUser'] = $user;
			

			_apiReturn(1, L('login').L('success'),$user,$url);
		}else{
			
			_apiReturn(0,"账户名或密码错误");
		}
		
	}
}